# -*- coding: utf-8 -*-
"""
Created on Mon Oct 28 21:19:34 2019

@author: 86187
"""

#类定义语法
class Car:
    def infor(self):
        print("This is a car")

car = Car()
car.infor()

print(isinstance(car,Car))
print(isinstance(car,str))

#pass占位符
#class A:
#    pass
class B:
    pass
class C:
    pass

#self参数
class A:
    def __init__(hahaha,v):
        hahaha.value = v
    def show(hahaha):
        print(hahaha.value)
a = A(3)
a.show()

class Car:
    price = 100000#定义类属性
    def __init__(self,c):
        self.color = c#定义实例属性

print('--------------------类成员与实例成员------------------------')
car1 = Car("Red")
car2 = Car("Blue")
print(car1.color,Car.price)
Car.price = 11000 #修改实例属性
Car.name = 'bmw'#增加类属性
car1.color = "Yellow"#修改实例属性
print(car2.color,Car.price,Car.name)
def setSpeed(self,s):
    self.speed = s
import types
car1.setSpeed = types.MethodType(setSpeed,car1)#动态为对象添加成员方法
car1.setSpeed(50)
print(car1.speed)
print('------------------------继承机制------------------------------')

class Person(object):#必须以object为基类
    def __init__(self,name='',age = 20,sex = 'man'):
        self.setName(name)
        self.setAge(age)
        self.setSex(sex)

    def setName(self,name):
        if not isinstance(name,str):
            print('name must be string.')
            return
        self.__name = name

    def setAge(self,age):
        if not isinstance(age,int):
            print('age must be integer.')
            return
        self.__age = age

    def setSex(self,sex):
        if sex != 'man' and sex != 'woman':
            print('sex must be "man" or "woman"')
            return
        self.__sex = sex

    def show(self):
        print('Name:',self.__name)
        print('Age:',self.__age)
        print('Sex:',self.__sex)

class Teacher(Person):#派生类
    def __init__(self,name='',age=30,sex='man',department='Computer'):
        super(Teacher,self).__init__(name,age,sex)
        self.setDepartment(department)
    def setDepartment(self,department):
        if not isinstance(department,str):
            print('department must be a string.')
            return
        self.__department = department
    def show(self):
        super(Teacher,self).show()
        print('Department:',self.__department)

if __name__ == '__main__':
    zhangsan = Person('Zhang san',19,'man')
    zhangsan.show()
    lisi = Teacher('Li Si',32,'man','Math')
    lisi.show()
    lisi.setAge(40)
    lisi.show()

